﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Jzy.Core.Common.Web;
using Jzy.Model.Contact;
using Jzy.BLL.CRM;
using Jzy.Model.CRM;
using Frame.Lib.Utilities;

namespace Jzy.Web.CRM.Contact
{
    public partial class List : PageList
    {
        private CustomerContactBO _contact;
        private CustomerContactBO Contact
        {
            get
            {
                if (_contact == null)
                {
                    _contact = new CustomerContactBO();
                }
                return _contact;
            }

        }

        private string CustomerID
        {
            get
            {
                return Request["CustomerID"];
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
          

            if (!IsPostBack)
            {
               
                BindView();
                
            }
            AddAttr();
            base.InitSelectID(hidSelectIDs);
           
            JSUtil.SetTableHeight("list", 145);

        }


        private void AddAttr()
        {
            JSUtil.DelConfirm(btnDelete, "联系人");

            JSUtil.ShowForm(lnkAdd, "增加联系人", null, "Edit.aspx?CustomerID=" + this.CustomerID, 440, 320);
            JSUtil.ClickBtn(lnkDelete, btnDelete);

        }

        private void BindView()
        {

            LVUtil.Init(lv, lvHeader);

            IEnumerable<CustomerContactInfo> list = this.Contact.GetList(int.Parse(this.CustomerID));

            lblStatistic.Text = "[&nbsp;共<b style='color:red;'> " + list.Count() + "</b>&nbsp;个联系人&nbsp;]";

            lv.DataSource = list;
            lv.DataKeyNames = new string[] { "ID" };
            lv.DataBind();


        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (strSelectIDs == "")
            {
                return;
            }

            //要删除相关的图片和附件
            this.Contact.LogicDelete(strSelectIDs);
            BindView();
        }

       

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindView();
        }

       


        protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                string strID = StrUtil.EncryptUrl(lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["ID"].ToString());
                HyperLink lnkEdit = (HyperLink)e.Item.FindControl("lnkEdit");
                HyperLink lnkName = (HyperLink)e.Item.FindControl("lnkName");

                string url = "Edit.aspx?ID=" + strID;
                JSUtil.ShowForm(lnkEdit,"修改联系人",null,url, 440, 320);
                JSUtil.ShowForm(lnkName, "修改联系人", null, url, 440, 320);
            }
        }

       
    }
}
